{% extends "admin/layout.html" %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card shadow mb-4">
                <div class="card-header py-3 d-flex justify-content-between align-items-center">
                    <h6 class="m-0 font-weight-bold text-primary">数据库备份列表</h6>
                    <a href="{{ url_for('admin.admin_config.database_config') }}" class="btn btn-sm btn-primary" aria-label="数据库配置">
                        <i class="fas fa-cog me-1"></i> 数据库配置
                    </a>
                </div>
                <div class="card-body">
                    {% if backups %}
                    <div class="table-responsive">
                        <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0" aria-label="数据库备份列表">
                            <thead>
                                <tr>
                                    <th>文件名</th>
                                    <th>大小</th>
                                    <th>修改时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for backup in backups %}
                                <tr>
                                    <td>{{ backup.filename }}</td>
                                    <td>{{ backup.size }}</td>
                                    <td>{{ backup.mtime }}</td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ url_for('admin.admin_backup.download_backup', filename=backup.filename) }}"
                                               class="btn btn-sm btn-success" aria-label="下载备份">
                                                <i class="fas fa-download"></i> 下载
                                            </a>
                                            <form method="POST" action="{{ url_for('admin.admin_backup.delete_backup', filename=backup.filename) }}"
                                                  class="d-inline">
                                                <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
                                                <button type="submit" class="btn btn-sm btn-danger"
                                                        data-confirm="确定要删除此备份吗？" aria-label="删除备份">
                                                    <i class="fas fa-trash"></i> 删除
                                                </button>
                                            </form>
                                            <button type="button" class="btn btn-sm btn-warning"
                                                    data-bs-toggle="modal" data-bs-target="#restoreModal"
                                                    data-filename="{{ backup.filename }}" aria-label="恢复备份">
                                                <i class="fas fa-undo"></i> 恢复
                                            </button>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i> 暂无备份文件
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 恢复确认模态框 -->
<div class="modal fade" id="restoreModal" tabindex="-1" aria-labelledby="restoreModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="restoreModalLabel">恢复数据库</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-warning">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <strong>警告：</strong> 此操作将用备份文件覆盖当前数据库，可能导致数据丢失！请谨慎操作。
                </div>
                <form id="restoreForm" method="POST" action="{{ url_for('admin.admin_backup.restore_backup') }}">
                    <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
                    <input type="hidden" name="backup_file" id="backupFileInput">
                    <div class="mb-3">
                        <label for="confirmationInput" class="form-label">请输入 "YES" 确认恢复操作：</label>
                        <input type="text" class="form-control" id="confirmationInput" name="confirmation" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="submit" form="restoreForm" class="btn btn-warning" aria-label="确认恢复数据库">确认恢复</button>
            </div>
        </div>
    </div>
</div>

<script>
    // 当模态框显示时，设置备份文件名
    var restoreModal = document.getElementById('restoreModal');
    restoreModal.addEventListener('show.bs.modal', function (event) {
        var button = event.relatedTarget;
        var filename = button.getAttribute('data-filename');
        var modalInput = document.getElementById('backupFileInput');
        modalInput.value = filename;
    });
</script>
{% endblock %}